package com.skiving.cloud.admin.service.impl;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.skiving.cloud.admin.entity.SysRoleMenu;
import com.skiving.cloud.admin.mapper.SysRoleMenuMapper;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.admin.entity.SysRole;
import com.skiving.cloud.admin.mapper.SysRoleMapper;
import com.skiving.cloud.admin.service.SysRoleService;
import com.skiving.cloud.common.utils.UuidUtil;
import com.skiving.cloud.constant.AdminConstant;
import org.noear.solon.annotation.Component;
import org.noear.solon.Utils;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Transaction;

import java.util.Arrays;
import java.util.List;

/**
 * 角色 服务层实现。
 *
 * @author skivingcloud
 * @since 2025-03-08
 */
@Component
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>  implements SysRoleService {
    @Inject
    private SysRoleMenuMapper sysRoleMenuMapper;
    /**
     * 分页查询角色
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysRole> queryByPage(PageParameter<SysRole> parameter){
        Page<SysRole> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (Utils.isNotBlank(parameter.getParam().getId())) {
                queryWrapper.and(SysRole::getId).like(parameter.getParam().getId());
            }
            if (Utils.isNotBlank(parameter.getParam().getRoleName())) {
                queryWrapper.and(SysRole::getRoleName).like(parameter.getParam().getRoleName());
            }
            if (Utils.isNotBlank(parameter.getParam().getRemark())) {
                queryWrapper.and(SysRole::getRemark).like(parameter.getParam().getRemark());
            }
            if (Utils.isNotBlank(parameter.getParam().getCreateUserId())) {
                queryWrapper.and(SysRole::getCreateUserId).like(parameter.getParam().getCreateUserId());
            }
            if (Utils.isNotBlank(parameter.getParam().getModUserId())) {
                queryWrapper.and(SysRole::getModUserId).like(parameter.getParam().getModUserId());
            }
            if (Utils.isNotBlank(parameter.getParam().getTenantId())) {
                queryWrapper.and(SysRole::getTenantId).like(parameter.getParam().getTenantId());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 根据租户id获取所有角色
     *
     * @param tenantId 租户id
     * @return 角色
     */
    @Override
    public List<SysRole> listByTenantId(String tenantId) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(SysRole::getTenantId).eq(tenantId);
        return this.list(queryWrapper);
    }

    /**
     * 设置角色资源权限
     *
     * @param id    角色ID
     * @param resId 资源权限ID
     */
    @Override
    @Transaction
    public void saveRoleRes(String id, String[] resId) {
        QueryWrapper sm = QueryWrapper.create();
        sm.where(SysRoleMenu::getRoleId).eq(id);
        sysRoleMenuMapper.deleteByQuery(sm);
        List<SysRoleMenu> list = Arrays.stream(resId).map(r -> {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setId(UuidUtil.get32UUID());
            sysRoleMenu.setRoleId(id);
            sysRoleMenu.setMenuId(r);
            return sysRoleMenu;
        }).toList();
        //批量插入
        Db.executeBatch(list, AdminConstant.BATCH_SIZE, SysRoleMenuMapper.class, BaseMapper::insert);
    }
}
